package com.codeape.number.ui.main.fragment.record;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.codeape.number.R;
import com.codeape.number.entity.Records;
import com.codeape.number.ui.AnnotationActivity;
import com.codeape.number.util.DatabaseUtils;
import com.codeape.number.util.Utils;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

public class RecordViewModel {

    RecordFragment fragment;

    private static ArrayList<Integer> mGuashuList = new ArrayList<>();
    private static ArrayList<String> mYaociList = new ArrayList<>();

    private static Cursor mGuaData, mRecordsData;

    public static ObservableList<Records> mRecordsList = new ObservableArrayList<>();
    public static ObservableField<RecordsAdapter> mRecordsAdapter = new ObservableField<>();

    RecordViewModel(RecordFragment fragment) {
        this.fragment = fragment;
        new operationTask(this).execute();
    }

    //建库线程
    private static class operationTask extends AsyncTask<String, Object, Long> {

        private final WeakReference<RecordViewModel> fragment;

        private operationTask(RecordViewModel fragment) {
            this.fragment = new WeakReference<>(fragment);
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Long doInBackground(String... strings) {

            // 获取表数据
            mGuaData = DatabaseUtils.getDatabaseTable(DatabaseUtils.getGuaDb(), "gua", false);

            mGuashuList.clear();
            for (int i = 1; i <= 8; i++) {
                for (int j = 1; j <= 8; j++) {
                    mGuashuList.add(i * 10 + j);
                }
            }

            for (int i = 1; i <= 64; i++) {
                for (int j = 1; j <= 6; j++) {
                    mYaociList.add(i + "-" + j);
                }
            }

            name();

            return null;
        }

        @Override
        protected void onPostExecute(Long aLong) {
            super.onPostExecute(aLong);
            RecordViewModel mFragment = fragment.get();
            mRecordsAdapter.set(new RecordsAdapter(mFragment.fragment.getActivity(), mRecordsList));
        }
    }

    static void name() {
        // 获取表数据
        mRecordsData = DatabaseUtils.getDatabaseTable(DatabaseUtils.getRecordsDb(), "records", true);
        mRecordsList.clear();
        for (int i = 0; i < mRecordsData.getCount(); i++) {
            mRecordsData.moveToPosition(i);
            for (int j = 0; j < mGuashuList.size(); j++) {
                if (mGuashuList.get(j) == Utils.AssemblyGua(Integer.valueOf(mRecordsData.getString(1).substring(0, 2)))) {
                    mGuaData.moveToPosition(j);
                }
            }
            mRecordsList.add(new Records(mRecordsData.getString(0), mRecordsData.getString(1), mGuaData.getString(1), mRecordsData.getString(2)));
        }
    }

    public static class RecordsAdapter extends BaseAdapter {

        private Context mContext;
        List<Records> mRecord;

        RecordsAdapter(Context context, List<Records> record) {
            this.mContext = context;
            this.mRecord = record;
        }

        @Override
        public int getCount() {
            if (null != mRecord) {
                return mRecord.size();
            }
            return 0;
        }

        @Override
        public Object getItem(int arg0) {
            return null;
        }

        @Override
        public long getItemId(int arg0) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                LayoutInflater inflater = LayoutInflater.from(mContext);
                convertView = inflater.inflate(R.layout.item_record, null);
                holder.records_rl = convertView.findViewById(R.id.item_rl_records);
                holder.record_image_main = convertView.findViewById(R.id.item_records_iv_main);
                holder.record_image_secondary = convertView.findViewById(R.id.item_records_iv_secondary);
                holder.record_tv_gua = convertView.findViewById(R.id.item_records_tv_gua);
                holder.record_tv_explain = convertView.findViewById(R.id.item_records_tv_explain);
                holder.record_tv_time = convertView.findViewById(R.id.item_records_tv_time);
                holder.view_divider = convertView.findViewById(R.id.v_divider);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.records_rl.setOnClickListener(view -> mContext.startActivity(new Intent(mContext, AnnotationActivity.class)
                    .putExtra("result", DatabaseUtils.getCalculationResult(mRecord.get(position).getResultCode()))));
            holder.record_image_main.setBackgroundResource(Utils.judgeGuaType(Integer.valueOf(mRecord.get(position).getResultCode().substring(0, 1))));
            holder.record_image_secondary.setBackgroundResource(Utils.judgeGuaType(Integer.valueOf(mRecord.get(position).getResultCode().substring(1, 2))));

            holder.record_tv_gua.setText(String.format("%s卦", mRecord.get(position).getGuaName()));
            holder.record_tv_explain.setText(String.format("变爻为%s", mRecord.get(position).getResultCode().charAt(2)));
            holder.record_tv_time.setText(mRecord.get(position).getTime());

            //隐藏最后一个分割线
//            if (position == mRecord.size() - 1) {
//                holder.view_divider.setVisibility(View.GONE);
//            }

            return convertView;
        }

        class ViewHolder {
            RelativeLayout records_rl;
            ImageView record_image_main, record_image_secondary;
            TextView record_tv_gua, record_tv_explain, record_tv_time;
            View view_divider;
        }

    }

}